<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生姓名" prop="dictName">
        <el-input v-model="queryParams.name" placeholder="请输入学生姓名" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="专业" prop="dictName">
        <el-input v-model="queryParams.major" placeholder="请输入学生专业" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="班级" prop="dictName">
        <el-input v-model="queryParams.className" placeholder="请输入班级" clearable style="width: 240px"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['student:student:add']">新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList">
      <el-table-column label="学生姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="学校名称" align="center" prop="schoolName" />
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="专业" align="center" prop="major" />
      <el-table-column label="爱好" align="center" prop="hobby" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['student:student:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['student:student:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="用户性别">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="学校名称">
          <el-input v-model="form.schoolName"  />
        </el-form-item>
        <el-form-item label="班级">
          <el-input v-model="form.className" />
        </el-form-item>
        <el-form-item label="专业">
          <el-input v-model="form.major" placeholder="请输入专业" />
        </el-form-item>
        <el-form-item label="爱好" >
          <el-input v-model="form.hobby" placeholder="请输入爱好" />
        </el-form-item>
        <el-form-item label="备注" >
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { selectStudentList, addStudent,updStudent,type,delStudent } from "@/api/student/student";
export default {
  name: "Student",
  dicts: ["sys_user_sex"],
  data() {
    return {
      //显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      //总条数
      total: 0,
      // 选中数组
      ids: [],
      //查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        major: undefined,
        className: undefined,
      },
      //新增属性
      title: "",
      dialogVisible: false,
      form: {
        name: undefined,
        major: undefined,
        className: undefined,
        sex: undefined,
        schoolName: undefined,
        hobby: undefined,
        id:undefined
      },
      studentList: [],
      // 表单参数
      form: {
        name: undefined,
        sex: undefined,
        major: undefined,
        schoolName: undefined,
        className: undefined,
        hobby: undefined,
        remark: undefined,
        id:undefined
      },
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
    
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        major: undefined,
        className: undefined
      },
        this.resetForm("queryForm");
        this.handleQuery();
    },
    /** 查询学生信息 */
    getList() {
      selectStudentList(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
     /** 新增按钮操作 */
     handleAdd() {
      this.reset();
      this.dialogVisible = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      type(id).then(response => {
        this.form = response.student;
        this.dialogVisible = true;
        this.title = "修改";
      });
    },
    // 取消按钮
    cancel() {
      this.dialogVisible= false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
      if (this.form.id!= undefined) {
        updStudent(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.getList();
        });
      } else {
        addStudent(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.getList();
        });
      }
    }
  });
    },
    reset(){
      this.form={
        id:undefined,
        name: undefined,
        major: undefined,
        classname: undefined,
        sex:undefined,
        schoolname:undefined,
        hobby:undefined
      };
      this.resetForm("form");
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const id = row.id;
      this.$modal.confirm('是否确认删除学生为"' + id + '"的数据项？').then(function () {
        return delStudent(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },

   
  }
};
</script>
<style scoped></style>